<?php

/*
 * Author: Trung
 * Filename: mycalendar.php
 * Created on Jul 27, 2008 3:22:04 PM
 */

require_once ('stdo.class.php');
require_once ('calendar.php');

class JBSCalendar extends Calendar {

	var $data;
	/*
	Array of Array of Item
	Array([1]=>Array(Item1, Item 2),
	Array([2]=>null,
	Array([3]=>Array(Item1, Item 2, Item3, Item 4),
	Array([4]=>null,
	Array([5]=>null
	................
	Array([31]=>null
	)*/
	function setData($data){
		$this->data = $data;
	}
	/*
	 * Insert Events to calendar
	 * */
	function getEventsInDate($day, $month, $year) {
		$content = '';
		$items = $this->data;

		foreach($items as $itemsOfTheDate){	//Duyet qua tat ca list
			foreach($itemsOfTheDate as $event){	//
				list($theYear, $theMonth, $theDay) = split('[/.-]', $event->today);
//				echo $theYear .'-'. $theMonth .'-'. $theDay .' = ['.$year .'-'. $month .'-'. $day.']';
				if($theYear === $year && $theMonth === $month && $theDay == $day) {
					$timeRange = $event->start_time != null && $event->end_time != null ? ($this->formatTime($event->start_time)) . "-" . ($this->formatTime($event->end_time)) : "";
					$content .= "<div class=\"event\" style=\"background: ".($event->use_master_color == 1 ? $event->master_color : $event->color)."\"  " .
								"onmouseover=\"".
								"return overlib('<table class=\'eventPopup\' border=\'0\' width=\'100%\' height=\'100%\'>".
								"	<tr>" .
								"		<td>";

					$content .= "<b>".JText::_('From').": </b>".JFactory::getDate($event->start_date)->toFormat('%A, %d %b %Y')."<br />";

					$content .= "<b>".JText::_('To').": </b>".JFactory::getDate($event->start_date)->toFormat('%A, %d %b %Y')."<br />".
								($event->is_allday_event != '1' ? '<b>'.JText::_('Time').':</b> '.$timeRange.'<br />' : '').
								"<small><em>".($event->is_allday_event == '1' ? JText::_('Allday event') : JText::_('Multiday event'))."</em></small>".
								"<hr />".$event->content.
								"		</td>" .
								"	</tr>" .
								"</table>');\" " .
								"onmouseout=\"return nd();\">" .
								"<div class=\"event_header\"><div class=\"event_title\">" . $timeRange . "</div>" .
								($event->recur == '1' ? "<div class=\"recur_flag\">&nbsp;</div>" : "" ) .
								($event->metting == '' ? "<div class=\"metting_flag\">&nbsp;</div>" : "" ) .
								"</div>" .
								"<div class=\"event_content\">".$event->subject."</div>" .
								"</div>" .
								"</div>";
				}
			}
		}
		return $content;
	}

	/*
	 * Insert Assets to calendar
	 * */
	function getAssetsInDate($day, $month, $year) {
		$content = '';
		$items = $this->data;
//		$content .= "<div style=\"width: 100%; height: 100%; overflow: auto\">&nbsp;";
		foreach($items as $itemsOfTheDate){	//Duyet qua tat ca list
			foreach($itemsOfTheDate as $asset){	//
				list($theYear, $theMonth, $theDay) = split('[/.-]', $asset->today);
				if($theYear === $year && $theMonth === $month && $theDay == $day) {
					$timeRange = $asset->start_time != null && $asset->end_time != null ? ($this->formatTime($asset->start_time)) . "-" . ($this->formatTime($asset->end_time)) : "";
					$content .= "<div class=\"asset\" style=\"position: relative; " .
								"top:".$this->time2pixel($this->time2float($asset->start_time))."px; " .
								"height:".$this->time2pixel($this->timeSubtract($asset->start_time, $asset->end_time))."px; " .
								"background: ".($asset->use_master_color == 1 ? $asset->master_color : $asset->color)."\" " .
								"onmouseover=\"".
								"return overlib('<table class=\'eventPopup\' border=\'0\' width=\'100%\' height=\'100%\'>".
								"	<tr>" .
								"		<td>";

					$content .= "<b>".JText::_('Time').":</b> $timeRange<br />" .
								"<hr /><b>$asset->subject</b><br />".$asset->content."<br />".
								(!empty($asset->img) ? "<img border=\'1\' align=\'middle\' src=\'images/tsb/".$asset->img."\'>" : "").
								"		</td>" .
								"	</tr>" .
								"</table>');\" " .
								"onmouseout=\"return nd();\">".
								//Insert Asset content here
								"</div>";
				}
			}
		}
//		$content .= "</div>";
		return $content;
	}

	function formatDate($date){
		return $date;
	}

	function formatTime($time){
		$dto = new SimpleDateTimeObject();
		$time_arr = $dto->Time2Array($time);
		$date = mktime ($time_arr['hours'],$time_arr['minutes'],$time_arr['seconds'],8,3,2008);
		return date('g:ia',$date);
	}

	/*
	 * Tạo link cho calendar của tháng xác định
	 * */
	function getCalendarLink($month, $year) {
		// Redisplay the current page, but with some parameters
		// to set the new month and year
		$link = getenv('SCRIPT_NAME');
		$link.= "?option=com_jbschedule&controller=calendar&month=$month&year=$year";
		$items = JRequest::getVar('items');
		if($items!="events" && $items!="assets")
			$items = "events";
		$display = JRequest::getVar('display');
		if($display!="week" && $display!="month" && $display!="year")
			$display = "month";

		$link.= "&items=".$items."&display=".$display;

		return $link;
	}

	/*
	 * Tạo link cho một ngày xác định
	 * */
	function getDateLink($day, $month, $year) {
		// Only link the first day of every month
		$link = "";
//		if ($day == 1) {
//			$link = "first.php";
//		}
		return $link;
	}

	function renderCalendarHeader($prevMonth, $nextMonth, $prevYear, $nextYear, $header){
//		".(JURI::root().'index.php?option=com_jbschedule&controller=calendar&display=week&items='.JRequest::getVar('items'))."
		$link = getenv('SCRIPT_NAME');
		$link.= "?option=com_jbschedule&controller=calendar";
		$todayLink = $link."&month=".date('n')."&year=".date('Y');

		$month = JRequest::getVar('month');
		if(!empty($month))
			$link .= "&month=$month";
		$year = JRequest::getVar('year');
		if(!empty($year))
			$link .= "&year=$year";

		$items = JRequest::getVar('items');
		if($items!='assets' && $items!='events')
			$items='events';
		$display = JRequest::getVar('display');
		if($display!='week' && $display!='month' && $display!='year')
			$display='month';


		$s .= "<table width=\"100%\" border=\"0\" cellpadding=\"0\" style=\"border-collapse: collapse;\">\n";
		$s .= "<tr>\n" .
			  "<td align=\"left\" valign=\"middle\">\n" .
				"<a class=\"calendarButton".($items=='assets'?'Active':'')."\" " .
						"href=\"".($link.'&display='.$display.'&items=assets')."\">".JText::_('ASSETS')."</a>\n" .
				"<a class=\"calendarButton".($items=='events'?'Active':'')."\" " .
						"href=\"".($link.'&display='.$display.'&items=events')."\">".JText::_('EVENTS')."</a>\n";
		$s .= "</td>\n";
		$s .= "<td align=\"left\" valign=\"middle\" width=\"60px\">\n" .
				(($prevMonth == "") ? "&nbsp;" :
					"<a class=\"calendarNav left2\" href=\"$prevYear\" title=\"".JText::_('Previous Year')."\" />\n" .
					"<a class=\"calendarNav left\" href=\"$prevMonth\" title=\"".JText::_('Previous Month')."\"/>\n") .
				"</td>\n";
		$s .= "<td align=\"center\"><a class=\"todayButton\" href=\"$todayLink&items=$items&display=$display\">".JText::_('Today')."</a></td>";
		$s .= "<td align=\"right\" valign=\"middle\" width=\"60px\">\n" .
				(($nextMonth == "") ? "&nbsp;" :
				"<a class=\"calendarNav right2\" href=\"$nextYear\" title=\"".JText::_('Next Month')."\" />\n" .
				"<a class=\"calendarNav right\" href=\"$nextMonth\" title=\"".JText::_('Next Year')."\" />\n") .
				"</td>\n";
		$s .= "<td align=\"center\" valign=\"middle\"><p class=\"calendarTitle\">".$header."</p></td>\n";
		$s .= "<td align=\"right\" valign=\"middle\">\n" .
				"<a class=\"calendarButton".($display=='week'?'Active':'')."\" " .
						"href=\"".($link.'&display=week&items='.$items)."\">".JText::_('WEEK')."</a>\n" .
				"<a class=\"calendarButton".($display=='month'?'Active':'')."\" " .
						"href=\"".($link.'&display=month&items='.$items)."\">".JText::_('MONTH')."</a>\n" .
				/*"<a class=\"calendarButton".($display=='year'?'Active':'')."\" " .
						"href=\"".($link.'&display=year&items='.$items)."\">".JText::_('YEAR')."</a>\n" .*/
				"</td>\n";
		$s .= "</tr>\n";
		$s .= "</table>\n<br />";

		return $s;
	}

	/*
	    Generate the HTML for a given month in JBS Standard
	*/
	function renderMonth($m, $y, $showYear = 1) {
		$s = "";

		$a = $this->adjustDate($m, $y);
		$month = $a[0];
		$year = $a[1];

		$daysInMonth = $this->getDaysInMonth($month, $year);
		$date = getdate(mktime(12, 0, 0, $month, 1, $year));

		$first = $date["wday"];
		$monthName = $this->monthNames[$month -1];

		if ($showYear == 1) {
			$prev = $this->adjustDate($month -1, $year);
			$next = $this->adjustDate($month +1, $year);

			$prevMonth = $this->getCalendarLink($prev[0], $prev[1]);
			$nextMonth = $this->getCalendarLink($next[0], $next[1]);

			$prev = $this->adjustDate($month, $year-1);
			$next = $this->adjustDate($month, $year+1);

			$prevYear = $this->getCalendarLink($prev[0], $prev[1]);
			$nextYear = $this->getCalendarLink($next[0], $next[1]);
		} else {
			$prevMonth = "";
			$nextMonth = "";
			$prevYear = "";
			$nextYear = "";
		}

		$header = $monthName . (($showYear > 0) ? " " . $year : "");
		$s .= $this->renderCalendarHeader($prevMonth, $nextMonth, $prevYear, $nextYear, $header);

		$s .= "<table class=\"calendar\" border=\"0\" style=\"border-collapse: collapse\">\n";
		$s .= "<tr>\n";
		$s .= "<td width=\"14.3%\" align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay) % 7] . "</td>\n";
		$s .= "<td width=\"14.3%\" align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay + 1) % 7] . "</td>\n";
		$s .= "<td width=\"14.3%\" align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay + 2) % 7] . "</td>\n";
		$s .= "<td width=\"14.3%\" align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay + 3) % 7] . "</td>\n";
		$s .= "<td width=\"14.3%\" align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay + 4) % 7] . "</td>\n";
		$s .= "<td width=\"14.3%\" align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay + 5) % 7] . "</td>\n";
		$s .= "<td width=\"14.3%\" align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay + 6) % 7] . "</td>\n";
		$s .= "</tr>\n";

		// We need to work out what date to start at so that the first appears in the correct column
		$d = $this->startDay + 1 - $first;
		while ($d > 1) {
			$d -= 7;
		}

		// Make sure we know when today is, so that we can use a different CSS style
		$today = getdate(time());

		while ($d <= $daysInMonth) {
			$s .= "<tr>\n";

			for ($i = 0; $i < 7; $i++) {
				$class = ($year == $today["year"] && $month == $today["mon"] && $d == $today["mday"]) ? "calendarToday" : "calendar";
				$s .= "<td height=\"70\" class=\"$class\" align=\"right\" valign=\"top\">";
				if ($d > 0 && $d <= $daysInMonth) {
					$s .= "<div class=\"dayNumber\">";
//					$link = $this->getDateLink($d, $month, $year);
					$s .= (($link == "") ? $d : "<a href=\"$link\">$d</a>");
					$s .= "</div>";
					//Insert JBS Events
					$s .= $this->getEventsInDate($d, $month, $year);
				} else {
					$s .= "<div class=\"dayDisabled\">&nbsp;</div>";
				}
				$s .= "</td>\n";
				$d++;
			}
			$s .= "</tr>\n";
		}

		$s .= "</table>\n";

		return $s;
	}
	/*
	    Generate the HTML for a given week in month in JBS Standard
	    @params:
	    	$week: accept value of int from 1 to 5
	    	$m:	month
	    	$y: year
	*/
	function renderWeek($m, $y, $week, $showYear = 1) {
		$s = "";

		$a = $this->adjustDate($m, $y);
		$month = $a[0];
		$year = $a[1];

		$daysInMonth = $this->getDaysInMonth($month, $year);
		$date = getdate(mktime(12, 0, 0, $month, 1, $year));

		$first = $date["wday"];
		$monthName = $this->monthNames[$month -1];

		//Show year
		$prev = $this->adjustDate($month -1, $year);
		$next = $this->adjustDate($month +1, $year);

		$prevMonth = $this->getCalendarLink($prev[0], $prev[1]);
		$nextMonth = $this->getCalendarLink($next[0], $next[1]);

		$prev = $this->adjustDate($month, $year-1);
		$next = $this->adjustDate($month, $year+1);

		$prevYear = $this->getCalendarLink($prev[0], $prev[1]);
		$nextYear = $this->getCalendarLink($next[0], $next[1]);

		$header = $monthName . (($showYear > 0) ? " " . $year : "");
		$s .= $this->renderCalendarHeader($prevMonth, $nextMonth, $prevYear, $nextYear, $header);

		$s .= "<table width=\"100%\" border=\"0\" style=\"border-collapse: collapse\">\n";
		$s .= "<tr>\n";
		$s .= "<td width=\"8%\" align=\"center\" valign=\"top\" class=\"calendarHeader\"></td>\n";
		$s .= "<td width=\"13%\" align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay) % 7] . "</td>\n";
		$s .= "<td width=\"13%\" align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay + 1) % 7] . "</td>\n";
		$s .= "<td width=\"13%\" align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay + 2) % 7] . "</td>\n";
		$s .= "<td width=\"13%\" align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay + 3) % 7] . "</td>\n";
		$s .= "<td width=\"13%\" align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay + 4) % 7] . "</td>\n";
		$s .= "<td width=\"13%\" align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay + 5) % 7] . "</td>\n";
		$s .= "<td width=\"13%\" align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay + 6) % 7] . "</td>\n";
		$s .= "<td width=\"3%\" align=\"center\" valign=\"top\" class=\"calendarHeader\"></td>\n";
		$s .= "</tr>\n";
		$s .= "</table>\n";

		$s .= "<div style=\"height: 450px; overflow: auto; border: 1px solid #ccc\">" .
		"<table id=\"calendar-week\" border=\"1\" bordercolor=\"#999\" " .
		"	width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" " .
		"	style=\"border-collapse: collapse; top: 200px\">" .
		"	<tr>" .
		"	    <td width=\"9%\" align=\"left\" valign=\"top\">";

		$s .= "<div id=\"timeline\">\n";
		//Draw the Timeline - 24 Hours
		for ($i = 0; $i < 24; $i++) {
			$s.= "<div class=\"timeline-wrapper\">\n" .
				"  <div class=\"timeline-hour\">".($i>12?($i-12):$i)."</div>\n" .
				"  <div class=\"timeline-min\">".($i==0?'AM':$i==12?'PM':'00')."</div>\n" .
				"  <div class=\"timeline-min\">30</div>\n" .
				"</div>\n";
		}
		$s .= "</div></td>\n";

		// We need to work out what date to start at so that the first appears in the correct column
		$d = $this->startDay + 1 - $first;
		while ($d > 1) {
			$d -= 7;
		}

		for ($i = 0; $i < 7; $i++) {
			$s .= "<td width=\"13%\" align=\"left\" valign=\"top\">";
				//Insert JBS Assets
				$s .= $this->getAssetsInDate($d, $month, $year);
			$s .= "</td>\n";
			$d++;
		}

		$s .= "</tr></table>\n";

		return $s;
	}

	/*
	 * Subtract time strings
	 * */
	function timeSubtract($time1, $time2){
		list ($h1, $m1, $s1) = split ('[:]', $time1);
		list ($h2, $m2, $s2) = split ('[:]', $time2);
		$value = ($h2 - $h1) + (($m2-$m1)/60);
//		echo "<li>timeSubtract($time1, $time2)=".$value."</li>";
		return $value;
	}
	function time2float($time){
		list ($h, $m, $s) = split ('[:]', $time);
		$value = $h + ($m/(60));
//		echo "<li>time2float($time)=".$value."</li>";;
		return $value;
	}
	/*
	 * Convert time to pixel to positioning items in calendar
	 * */
	function time2pixel($time){
		$hour = 32;	//32 pixel is an hour
		$value = $hour * $time;
//		echo "<li>time2pixel($time)=".$value."</li>";
		return $value;
	}
}
?>
